//
//  GZResetPasswordTwoViewController.swift
//  GZMarathon
//
//  Created by apple on 2021/11/11.
//

import UIKit
import SVProgressHUD
class GZResetPasswordTwoViewController: EBBaseViewController {


    @IBOutlet weak var subBt: UIButton!
    @IBOutlet weak var repdTF: UITextField!
    @IBOutlet weak var pdTF: UITextField!
    
    var yzm:String = ""
    var phone_num:String = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "重置密码"
        
        repdTF.delegate = self
        pdTF.delegate = self

        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        btn1.setImage(UIImage(named: "back"), for: .normal)
        btn1.addTarget(self, action: #selector(backClick), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn1)
        self.navigationItem.leftBarButtonItem = item1
        setUI()
       
    }
    
    @objc func backClick(){
        self.navigationController?.popViewController(animated: true)
    }

    func setUI(){
        
        
        let leftview = UIImageView(image: UIImage(named: "icon_denglu_mima"))
        leftview.frame = CGRect(x: 0, y: 0, width: 18, height: 18)
        repdTF.leftViewMode = UITextField.ViewMode.always
        repdTF.leftView = leftview
        
        let leftview1 = UIImageView(image: UIImage(named: "icon_denglu_mima"))
        leftview1.frame = CGRect(x: 0, y: 0, width: 18, height: 18)
        pdTF.leftViewMode = UITextField.ViewMode.always
        pdTF.leftView = leftview1

        
    }
    
    @IBAction func subClick(_ sender: UIButton) {
        
        view.endEditing(true)
        
        if(pdTF.text?.count == 0){
            SVProgressHUD.showError(withStatus: "密码不能为空")
            return
        }
        if(repdTF.text?.count == 0){
            SVProgressHUD.showError(withStatus: "密码不能为空")
            return
        }
        
        if(repdTF.text != pdTF.text){
            SVProgressHUD.showError(withStatus: "密码不一致")
            return
        }

        ApiProvider.request(EBAPI.modPwdByCaptcha(phone_num: phone_num, captcha: yzm, password: pdTF.text ?? ""), model: BaseModel.self) { res, isSuccess in
            
            if(isSuccess == true){
                if(res?.code == 200){
                    SVProgressHUD.showSuccess(withStatus: res?.message)
                    self.navigationController?.popToRootViewController(animated: true)
                }
            }
            
        }
        
    }

}

extension GZResetPasswordTwoViewController:UITextFieldDelegate{
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if((repdTF.text?.count != 0) && (pdTF.text?.count != 0)){
            subBt.backgroundColor = UIColor("#2E4BF4")
            subBt.isUserInteractionEnabled = true
        }else{
            subBt.isUserInteractionEnabled = false
            subBt.backgroundColor = UIColor("#C1C9FC")
        }
    }
    
}
